package com.wpf.servlet;

import com.wpf.bean.Pagebean;
import com.wpf.bean.Userbean;
import com.wpf.dao.UserDAO;
import java.io.IOException;
import java.util.ArrayList;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

public class DeleteData extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public DeleteData() {}
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		UserDAO userdao = new UserDAO();
		String ID = request.getParameter("id");
		String userName = request.getParameter("username");
		String loginName = (String) session.getAttribute("username");

		if(ID==""||ID==null) {
			session.setAttribute("operationtip","<font size='2' color='red'>删除错误！</font>");
		}else if (loginName.equals(userName)){
			session.setAttribute("operationtip","<font size='2' color='red'>在线登录时无法删除！</font>");
		}else {
			userdao.delete(Integer.parseInt(ID));
			session.setAttribute("operationtip","<font size='2' color='orange'>删除成功！</font>");
			Pagebean pagebean =   (Pagebean) session.getAttribute("pageresult");
			ArrayList<Userbean> list = pagebean.getRowSet();
			int idNum = Integer.parseInt(ID);
			int dbAllRow = pagebean.getDbAllRow();
			int pageSize = pagebean.getPageSize();
			int pageCount = pagebean.getPageCount();
			int pageNow = pagebean.getPageNow();
			pagebean.setDbAllRow(dbAllRow-1);
			int newPageCount =  userdao.pageMess(list,dbAllRow-1,pageSize);
			pagebean.setPageCount(newPageCount);
			int newPageNow;
			if(newPageCount<pageCount&&pageNow==pageCount) {
				newPageNow = pageNow-1;
			}else {
				newPageNow = pageNow;
			}
			pagebean.setPageNow(newPageNow);
			for(int i=0;i<list.size();i++) {
				if(list.get(i).getId()==idNum) {
					list.remove(i);
				}
			}
			if(list.size()==0||list==null) {
				session.setAttribute("operationtip","<font size='2' color='red'>系统无数据</font>");
			}else {
				StringBuffer queryAllBuffer = new StringBuffer();
				UserDAO.bufferAppend(queryAllBuffer);
				if(newPageCount==1) {//不分页情况下
					for(int i=0;i<dbAllRow-1;i++) {
						UserDAO.bufferAppend(queryAllBuffer,list,i);
					}
				}else if(newPageCount>1) {//分页情况下
					if(newPageCount<pageCount&&pageNow==pageCount) {//删除后页数发生变化，在最后一页删除仅剩最后一条数据
						int startpage1 = pageSize*(newPageNow-1);
						for(int i=startpage1;i<startpage1+pageSize && i<dbAllRow-1;i++) {
							UserDAO.bufferAppend(queryAllBuffer,list,i);
						}
					}else
						if(newPageCount<pageCount&&pageNow!=pageCount) {//删除后页数发生变化，在非最后一页删除一条数据
						int startpage2 = pageSize*(pageNow-1);
						for(int i=startpage2;i<startpage2+pageSize && i<dbAllRow-1;i++) {
							UserDAO.bufferAppend(queryAllBuffer,list,i);
						}
					}else if(newPageCount==pageCount){//删除后页数没有发生变化
						int startpage3 = pageSize*(newPageNow-1);
						for(int i=startpage3;i<startpage3+pageSize && i<dbAllRow-1;i++) {
							UserDAO.bufferAppend(queryAllBuffer,list,i);
						}
					}
				}
				queryAllBuffer.append("</table>");
				pagebean.setQueryResult(new String(queryAllBuffer));
				session.setAttribute("pageresult", pagebean);
			}
		}
		request.getRequestDispatcher("/wpf_index.jsp").forward(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 doGet(request, response);
	}
}
